package com.karaoke.android.home.widget

import android.annotation.TargetApi
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.media.AudioManager
import android.os.Build
import android.view.View
import android.view.ViewAnimationUtils
import com.dewarder.akommons.getService
import com.dewarder.akommons.layoutInflater
import com.karaoke.android.data.VolumeChangedEvent
import com.karaoke.android.data.masterVolume
import com.karaoke.android.home.databinding.VolumeCtrlViewBinding
import com.karaoke.android.player.VideoManager
import com.labo.kaji.relativepopupwindow.RelativePopupWindow
import org.simple.eventbus.EventBus
import org.simple.eventbus.Subscriber

/**
 * Created by Allen on 2016/10/16.
 */
class VolumePopup(context: Context) : RelativePopupWindow(context), SeekArc.OnSeekArcChangeListener
{
    override fun onProgressChanged(seekArc: SeekArc, progress: Int, fromUser: Boolean)
    {
        if (fromUser)
        {
            VideoManager.setVolume(progress / 100.000f)
        }
    }

    override fun onStartTrackingTouch(seekArc: SeekArc)
    {
    }

    override fun onStopTrackingTouch(seekArc: SeekArc)
    {
    }

    private var binding: VolumeCtrlViewBinding = VolumeCtrlViewBinding.inflate(context.layoutInflater)

    init
    {
        contentView = binding.root
        width = 210
        height = 210
        isFocusable = false
        isOutsideTouchable = true
        setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        contentView.measure(210, 210)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            animationStyle = 0
        }
        binding.seekArc.setOnSeekArcChangeListener(this)
    }

    override fun showOnAnchor(anchor: View, vertPos: Int, horizPos: Int, x: Int, y: Int)
    {
        super.showOnAnchor(anchor, vertPos, horizPos, x, y)
    }


//    @Subscriber
//    fun onVolumeChanged(e: VolumeChangedEvent)
//    {
//        binding.seekArc.progress = e.value
//    }

    override fun dismiss()
    {
        super.dismiss()
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private fun circularReveal(anchor: View)
    {
        contentView.apply {
            post {
                val myLocation = IntArray(2).apply { getLocationOnScreen(this) }
                val anchorLocation = IntArray(2).apply { anchor.getLocationOnScreen(this) }
                val cx = anchorLocation[0] - myLocation[0] + anchor.width / 2
                val cy = anchorLocation[1] - myLocation[1] + anchor.height / 2

                measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
                val dx = Math.max(cx, measuredWidth - cx)
                val dy = Math.max(cy, measuredHeight - cy)
                val finalRadius = Math.hypot(dx.toDouble(), dy.toDouble()).toFloat()
                ViewAnimationUtils.createCircularReveal(this, cx, cy, 0f, finalRadius).apply {
                    duration = 200
                    start()
                }
            }
        }
    }

    override fun showOnAnchor(anchor: View, vertPos: Int, horizPos: Int)
    {
        super.showOnAnchor(anchor, vertPos, horizPos)
        binding.seekArc.progress = (VideoManager.videoVolume * 100).toInt()
    }
}